package com.grj.parking.drools.a;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grj.parking.drools.domain.ParkingFeeRule;
import com.grj.parking.drools.mapper.ParkingFeeRuleMapper;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingFeeService {

    @Autowired
    private ParkingFeeRuleMapper ruleMapper; // MyBatis  mapper，查询 parking_fee_rule 表
    @Autowired
    private KieContainer kieContainer;

    /**
     * 计算停车费用
     * @param request 停车请求（租户ID、停车场ID、车位类型、入场/离场时间）
     * @return 费用计算结果
     */
    public String calculateFee(Long request) {
        // 1. 初始化结果对象

        // 2. 查询当前停车场的所有有效规则（租户+停车场）
        List<ParkingFeeRule> rules = ruleMapper.selectList(new QueryWrapper<ParkingFeeRule>().eq("lot_id", request));
        KieSession session = kieContainer.newKieSession();

        //Fact对象，事实对象
        Order order = new Order();
        order.setAmout(request);

        //将Order对象插入到工作内存中
        session.insert(order);

        //激活规则，由Drools框架自动进行规则匹配，如果规则匹配成功，则执行当前规则
        session.fireAllRules();
        //关闭会话
        session.dispose();




        return "feeResult";
    }
}